<?php

namespace app\controllers;
use yii\web\Controller;
use app\models\Uc_user;
use Yii;

use app\models\UploadFormw;
use yii\web\UploadedFile;
// var url = "http://42.96.185.45:8081/chuangwu/web/index.php?r=";
// var url = "http://115.28.48.128/chuangwu/web/index.php?r=";
$url = "http://localhost/chuangwu/web/index.php?r=";
class LeadinvapplyController extends Controller
{

    public function actionShow(){

        if(!empty($_COOKIE['user_id'])){
            $user_id = $_COOKIE['user_id'];
            $user = Uc_user::find()->where([
                'user_id' => $user_id,
                'del_flag' => 0
            ])->one();
            if($user['is_leadInvestor'] == 1){
                $this->redirect(['qulification/show']);
            }
            if($user['is_investor'] == 0){

                echo "<script>alert(\"请先成为投资者\");</script>";
                echo "<script>location.href = \"http://localhost/chuangwu/web/index.php?r=qulification/show\";</script>";

            }
            $view = Yii::$app->view;
            $view->params['title'] = "申请成为领投人";
            PrlikeController::setView($user);

            $model = new UploadFormw();
            if (Yii::$app->request->isPost) {
                $model->file1 = UploadedFile::getInstance($model, 'file1');
                $model->file2 = UploadedFile::getInstance($model, 'file2');
                $model->file3 = UploadedFile::getInstance($model, 'file3');
                $model->file4 = UploadedFile::getInstance($model, 'file4');
                $model->file5 = UploadedFile::getInstance($model, 'file5');

                $collection = "";
                if ($model->validate()) {
                    if($model->file1){

                        $model->file1->saveAs('uploads/' . $model->file1->baseName . '.' . $model->file1->extension);
                        $collection = $collection . 'uploads/' . $model->file1->baseName . '.' . $model->file1->extension . ';';
                    }

                    if($model->file2) {
                        $model->file2->saveAs('uploads/' . $model->file2->baseName . '.' . $model->file2->extension);
                        $collection = $collection . 'uploads/' . $model->file2->baseName . '.' . $model->file2->extension . ';';

                    }
                    if($model->file3) {
                        $model->file3->saveAs('uploads/' . $model->file3->baseName . '.' . $model->file3->extension);
                        $collection = $collection . 'uploads/' . $model->file3->baseName . '.' . $model->file3->extension . ';';

                    }
                    if($model->file4) {
                        $model->file4->saveAs('uploads/' . $model->file4->baseName . '.' . $model->file4->extension);
                        $collection = $collection . 'uploads/' . $model->file4->baseName . '.' . $model->file4->extension . ';';
                    }
                    if($model->file5){
                        $model->file5->saveAs('uploads/' . $model->file5->baseName . '.' . $model->file5->extension);
                        $collection = $collection . 'uploads/' . $model->file5->baseName . '.' . $model->file5->extension . ';';

                    }

                    $query = Uc_qualification::findOne([
                        'user_id' => $user_id,
                        'del_flag' => 0,
                    ]);
                    if($query == null){
                        echo "<script>alert('您还不是投资者')</script>";
                        echo "<script>location.href = \"http://localhost/chuangwu/web/index.php?r=qulification/show\";</script>";
                    }else{
                        $query['qulityImage'] = $collection;
                        $query['type'] = 2;
                        $query->save();
                    }


                    echo "<script>alert('您的申请已经提交...请耐心等待')</script>";
                    echo "<script>location.href = \"http://localhost/chuangwu/web/index.php?r=qulification/show\";</script>";
                }

            }

            return $this->render('leadinvapply',  ['user'=>$user, 'model'=>$model]);

        }else{
            echo "<script>alert('cookie 不存在')</script>";
            $this->redirect(['login/show']);
        }


    }
    public function actionLogout(){

        setcookie('user_id','',time()-3600);
//        echo "<script>alert('logout');</script>";
        $this->redirect(['index/show']);
    }

}